package com.gosophia.metadataEngine.service.impl;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.gosophia.commons.OrderCondition;
import com.gosophia.commons.Page;
import com.gosophia.commons.exception.BusinessException;
import com.gosophia.commons.exception.ConcurrencyException;
import com.gosophia.commons.exception.ExceptionDetail;
import com.gosophia.metadataEngine.commons.CreateXML;
import com.gosophia.metadataEngine.commons.DBUtil;
import com.gosophia.metadataEngine.commons.SessionUtil;
import com.gosophia.metadataEngine.dao.DataSourceInfoDao;
import com.gosophia.metadataEngine.entity.DataSourceInfo;
import com.gosophia.metadataEngine.entity.MetaDataTable;
import com.gosophia.metadataEngine.service.DataSourceInfoService;
/**
 * 数据源impl
 * @author mayue
 *
 */
@Service("dataSourceInfoService")
@Transactional
public class DataSourceInfoServiceImpl implements 
        DataSourceInfoService {
    @Autowired
    private DataSourceInfoDao dataSourceInfoDao;
    public void addDataSourceInfo(DataSourceInfo dataSourceInfo)
            throws BusinessException {
        Assert.notNull(dataSourceInfo,"dataSourceInfo不能为null");
        Assert.notNull(dataSourceInfo.getName(),"name不能为null");
        Assert.notNull(dataSourceInfo.getDriverClass(),"driverClass不能为null");
        Assert.notNull(dataSourceInfo.getDialect(),"dialect不能为null");
        Assert.notNull(dataSourceInfo.getUrl(),"url不能为null");
        Long count = dataSourceInfoDao.countNamedQueryResult("DataSourceInfo.findBySameName", dataSourceInfo.getName());
        if(count>0){
            throw new BusinessException("名称不能重复！","JC2_YSJ_B_1");
        }
        //创建时间为当前时间
        dataSourceInfo.setCreateTime(new Date());
        dataSourceInfoDao.save(dataSourceInfo);
    }

    public Page<DataSourceInfo> findAllDataSourceInfoByPage(Page<DataSourceInfo> page,String searchString,List<OrderCondition> orderConditions) {
    	Assert.notNull(page,"page不能为null");
        Assert.isTrue(page.getPageSize() > 0, "pageSize必须大于0");
        Assert.isTrue(page.getPageNo() > 0, "pageNo必须大于0");
        return dataSourceInfoDao.getPageBySimpleSearching(page, searchString, orderConditions);
    }

    public DataSourceInfo removeDataSourceInfo(Long id, Long version)
            throws BusinessException {
       Assert.notNull(id,"id不能为null");
       Assert.notNull(version,"version不能为null");
       DataSourceInfo dsi = dataSourceInfoDao.get(id);
       if(dsi == null){
           throw new ConcurrencyException("该数据源已被删除！",new ExceptionDetail("JC2_YSJ_C_1", "该数据源已被删除！"));
       }
       if(!version.equals(dsi.getVersion())){
           throw new ConcurrencyException("该数据源已被修改！",new ExceptionDetail("JC2_YSJ_C_2", "该数据源已被修改！"));
       }
       try {
           dataSourceInfoDao.delete(id, version);
           return dsi;
       } catch (Exception e) {
           throw new BusinessException("该数据源被引用，不能删除！","JC2_YSJ_B_2");
       }
       
    }

    public void updateDataSourceInfo(DataSourceInfo dataSourceInfo)
            throws BusinessException {
        Assert.notNull(dataSourceInfo,"dataSourceInfo不能为null");
        Assert.notNull(dataSourceInfo.getName(),"name不能为null");
        Assert.notNull(dataSourceInfo.getDriverClass(),"driverClass不能为null");
        Assert.notNull(dataSourceInfo.getDialect(),"dialect不能为null");
        Assert.notNull(dataSourceInfo.getPassword(),"password不能为null");
        Assert.notNull(dataSourceInfo.getUsername(),"userName不能为null");
        Assert.notNull(dataSourceInfo.getUrl(),"url不能为null");
        DataSourceInfo dsi = dataSourceInfoDao.get(dataSourceInfo.getDataSourceInfoId());
        if(dsi == null){
            throw new  ConcurrencyException("该数据源已被删除！",new ExceptionDetail("JC2_YSJ_C_3", "该数据源已被删除！"));
         }
        if(!dataSourceInfo.getVersion().equals(dsi.getVersion())){
             throw new ConcurrencyException("该数据源已被修改！",new ExceptionDetail("JC2_YSJ_C_4","该数据源已被修改！"));
         }
        Long count = dataSourceInfoDao.countNamedQueryResult("DataSourceInfo.findBySameNameNotId", dataSourceInfo.getName(),dataSourceInfo.getDataSourceInfoId());
        if(count>0){
            throw new BusinessException("名称不能重复！","JC2_YSJ_B_3");
        }
        dataSourceInfo.setCreateTime(dsi.getCreateTime());
        dataSourceInfoDao.update(dataSourceInfo);
    }

    public DataSourceInfo findDataSourceInfoById(Long dataSourceInfoId) {
        Assert.notNull(dataSourceInfoId,"dataSourceInfoId不能为null");
        return dataSourceInfoDao.get(dataSourceInfoId);
    }

	@Override
	public boolean checkDataSourceInfo(DataSourceInfo dataSourceInfo) {
		Assert.notNull(dataSourceInfo,"dataSourceInfo不能为null");
		Assert.notNull(dataSourceInfo.getDialect(),"dataSourceInfo.dialect不能为null");
		Assert.notNull(dataSourceInfo.getDriverClass(),"dataSourceInfo.driverClass不能为null");
		Assert.notNull(dataSourceInfo.getPassword(),"dataSourceInfo.password不能为null");
		Assert.notNull(dataSourceInfo.getUsername(),"dataSourceInfo.userName不能为null");
		Assert.notNull(dataSourceInfo.getUrl(),"dataSourceInfo.url不能为null");
		return DBUtil.isConnectioned(dataSourceInfo);
		/*boolean ifTrue = false;
        try{
        	org.dom4j.Document doc = CreateXML.createDataSourceXml(dataSourceInfo);
        	SessionUtil.getSessionFactory("测试",doc,new ArrayList<String>());
        	ifTrue = true;
        }catch(Exception e){
        	ifTrue= false;
        	e.printStackTrace();
        }finally{
    		return ifTrue;
        }*/
	}

}
